<?php
namespace app\money\controller;
use app\admin\common\Base;
use think\Db;
//现金银行报表
class Cashbank extends Base
{
    public function index()
    {
        $data = $this->request->param();
 		$month = $this->get_month_start_to_end();
      	$this->assign('month', $month);
        if (isset($data['ajax']) && $data['ajax'] == 100) {
        	$search=[];
        	$where=[];
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if (isset($data['startdate']) && isset($data['enddate'])) {
	            $month = $this->get_month_start_to_end($data['startdate'], $data['enddate']);
	        } else {
	            $month = $this->get_month_start_to_end();
	        }

	     	 //备注
	        if (isset($data['mark']) && $data['mark'] != '') {
	             $search['mark'] =['like',"%".$data['mark']."%"]; 
	        }
	        //结算账户
	        if (isset($data['account_id']) && $data['account_id'] != '') {
	            $where['account_id']= ['in', $data['account_id']];
	        }
	         //业务类型
	        if (isset($data['catorage_id']) && $data['catorage_id'] != '') {
	            $search['o_type']= ['in', $data['catorage_id']];
	        }
	       
	        $alist=[];
 			$acc=Db::name('account_record')->where($where)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->distinct(true)->field('account_id')->order('account_id asc')->select();
 			foreach ($acc as $ke => &$va) {
 				$account=Db::name('account')->where('id',$va['account_id'])->find();
            	$va['account_sn']=$account['sn'];
            	$va['account_name']=$account['name'];
            	$va['money']=$this->account_time_money($month['month_start'],$va['account_id']);
            	$va['o_type_name']="期初余额";
            	array_push($alist, $va);

            	// *************************
            	
	            $list =  Db::name('account_record')->where($search)->where('account_id', $va['account_id'])->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('addtime', '>=', $month['month_start'])->where('addtime', '<=', $month['month_end'])->order('account_id asc,addtime asc,id asc')->select();
	             
	               
	          
	            $money=$va['money'];
	            foreach ($list as $key => $val) {
	            	$account=Db::name('account')->where('id',$val['account_id'])->find();
	            	$val['account_sn']=$account['sn'];
	            	$val['account_name']=$account['name'];
	                
	                $val['add_time'] = date('Y-m-d', $val['addtime']); 
	                switch ($val['o_type']) {
						case 0:
							$val['o_type_name']="收款";
							break;
						case 1:
							$val['o_type_name']="付款";
							break;
						case 2:
							$val['o_type_name']="普通销售";
							break;
						case 3:
							$val['o_type_name']="销售退回";
							break;
						case 4:
							$val['o_type_name']="普通采购";
							break;
						case 5:
							$val['o_type_name']="采购退回";
							break;
						default:
							$val['o_type_name']="其它";
							break;
					}
					//收款，销售，采购退货
					if(in_array($val['o_type'], [0,2,5])){
						$val['r']=$val['account_money'];
						$val['c']='';
						$money+=floatval($val['account_money']);
					}else{
						$val['r']='';
						$val['c']=$val['account_money'];
						$money-=floatval($val['account_money']);
					}
					$val['money']=sprintf("%.2f",$money);
					if(in_array($val['o_type'], [0,1])){//收、付款单
						$aainfo=Db::name('payment')->where('sn',$val['psn'])->find();
						
						$val['saler_id']=Db::name('auth_user')->where('id',$aainfo['saler'])->value('id');
						$val['saler']=Db::name('auth_user')->where('id',$aainfo['saler'])->value('name');
						$val['user_id']=Db::name('customer')->where('id',$aainfo['user_id'])->value('id');
						$val['company']=Db::name('customer')->where('id',$aainfo['user_id'])->value('name');
					}else if (in_array($val['o_type'], [2,3])) {//销售

						$aainfo=Db::name('order')->where('sn',$val['psn'])->find();
						
						$val['saler_id']=Db::name('auth_user')->where('id',$aainfo['saler'])->value('id');
						$val['saler']=Db::name('auth_user')->where('id',$aainfo['saler'])->value('name');
						$val['user_id']=Db::name('customer')->where('id',$aainfo['user_id'])->value('id');
						$val['company']=Db::name('customer')->where('id',$aainfo['user_id'])->value('name');
					}elseif (in_array($val['o_type'], [4,5])) {//购货
						$aainfo=Db::name('invpu')->where('sn',$val['psn'])->find();
						
						$val['saler_id']=Db::name('auth_user')->where('id',$aainfo['by_admin'])->value('id');
						$val['saler']=Db::name('auth_user')->where('id',$aainfo['by_admin'])->value('name');
						$val['user_id']=Db::name('customer')->where('id',$aainfo['supplier_id'])->value('id');
						$val['company']=Db::name('customer')->where('id',$aainfo['supplier_id'])->value('name');
					}
					//$val['money']=$this->account_time_money($val['addtime']);
					$flag=0;
					//客户
	            	if (isset($data['user_id']) && $data['user_id'] != '') {
	            		$arr_p=explode(',', $data['user_id']);
	            		
	            		if ($val['o_type_name']!='期初余额'&&!in_array($val['user_id'], $arr_p)) {
	            			$flag+=1;
	            		}
	            	}
					//经销商
	            	if (isset($data['supplier_id']) && $data['supplier_id'] != '') {
	            		$arr_p=explode(',', $data['supplier_id']);
	            		if ($val['o_type_name']!='期初余额'&&!in_array($val['user_id'], $arr_p)) {
	            			$flag+=1;
	            		}
	            	}
	            	//收付款人
	            	if (isset($data['staff_id']) && $data['staff_id'] != '') {
	            		$arr_p=explode(',', $data['staff_id']);
	            		if ($val['o_type_name']!='期初余额'&&!in_array($val['saler_id'], $arr_p)) {
	            			$flag+=1;
	            		}
	            	}
	            	
            		if($flag==0){
            			array_push($alist, $val);
            		}
					
	            }
            	//**************************
 			}
            $count = count($alist);
            return $this->grid_data($alist, $count, $page, $limit);
        }
        $today=date('Y-m-d',time());
        $mon_start=date('Y-m',time()).'-01';
        $this->assign('today',$today);
        $this->assign('mon_start',$mon_start);
      
        return $this->fetch();
    }


	//账户记录--根据时间查询当前时间金额
	//$type 0包含当前时间 ，1不包含当前时间
	function account_time_money($time,$account_id)
	{
		
		$list =  Db::name('account_record')->where('is_del', 0)->where('account_id', $account_id)->where('enterprise_id', $this->EnterpriseId)->where('addtime', '<', $time)->order('addtime asc,id asc')->select();
		
		$money=0;
		//halt($list);
		foreach ($list as $key => $val) {
			//收款，销售，采购退货
			if(in_array($val['o_type'], [0,2,5])){
				$money+=floatval($val['account_money']);
				
			}else{
				$money-=floatval($val['account_money']);
			}
		}
		return sprintf("%.2f",$money);
	}




}
